package com.movie.ticket.sale.movieassembly.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.ticket.sale.movieassembly.controller.form.*;
import com.movie.ticket.sale.movieassembly.controller.form.TMovie;
import com.movie.ticket.sale.moviecommon.model.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface MovieDetailsMapper extends BaseMapper<TMovieData> {
    /**
     * 展示电影封面
     */
    @Select("select md.file_path FROM t_movie AS m JOIN t_movie_data as md on m.id=md.movie_id WHERE m.`id`=#{id} AND md.type=0")
    String getMovieData(int id);

    /**
     * 展示电影信息(包括简介)
     */
    @Select("select * from t_movie where id=#{id}")
    TMovie getMovieInnformention(int id);

    /**
     * 查询当前电影的评论总条数
     */
    @Select("SELECT count(c.id) FROM t_movie AS m JOIN t_comment AS c ON m.id=c.movie_id where m.`id`=#{id}")
    int getMovieReviewNumber(int id);

    /**
     * 分页展示电影影评
     *
     * @param id
     * @param
     * @param
     * @return
     */
    @Select("SELECT c.id,u.`name`,c.`comment`,c.degree FROM " +
            "t_movie AS m JOIN t_comment AS c JOIN t_user AS u ON m.id=c.movie_id AND u.id=c.user_id " +
            "where m.`id`=#{id}")
    IPage<MovieCommon> getMovieReview(Page<MovieCommon> page, @Param("id") int id);

    /**
     * 展示导演头像
     *
     * @param id
     * @return
     */
    @Select("SELECT  p.`name`,pd.path as headPicture \n" +
            "FROM t_movie AS m JOIN t_movie_person AS mp JOIN t_person AS p JOIN t_person_data AS pd \n" +
            "ON m.id=mp.movie_id AND mp.person_id=p.id AND p.id=pd.person_id WHERE m.`id`=#{id} AND mp.type='0'")
    List<MoviePerson> getMoviePersonDirector(int id);

    /**
     * 展示演员头像
     *
     * @param id
     * @return
     */
    @Select("SELECT  p.`name`,pd.path \n" +
            "FROM t_movie AS m JOIN t_movie_person AS mp JOIN t_person AS p JOIN t_person_data AS pd \n" +
            "ON m.id=mp.movie_id AND mp.person_id=p.id AND p.id=pd.person_id WHERE m.`id`=#{id} AND mp.type='1'")
    List<MoviePerson> getMoviePersonActor(int id);

    /**
     * 获取电影剧照
     *
     * @param id
     * @return
     */
    @Select("SELECT md.file_path FROM t_movie AS m JOIN t_movie_data AS md on m.id=md.movie_id WHERE m.`id`=#{id} AND md.type='1'")
    List<String> getMoviePhoto(int id);

    /**
     * 展示演员头像
     *
     * @param id
     * @return
     */
    @Select("SELECT pd.path FROM t_person AS p JOIN t_movie_person AS mp JOIN t_person_data pd ON p.id=mp.person_id AND p.id=pd.person_id WHERE p.`id`=#{id} AND mp.type='0'")
    String getCastPhotoAuctor(int id);

    /**
     * 展示导演头像
     *
     * @param id
     * @return
     */
    @Select("SELECT pd.path FROM t_person AS p JOIN t_movie_person AS mp JOIN t_person_data pd ON p.id=mp.person_id AND p.id=pd.person_id WHERE p.`id`=#{id} AND mp.type='1'")
    String getCastPhotoDirector(int id);

    /**
     * 查询所有影院
     *
     * @return
     */
    @Select("select * from t_cinema where degree='2'")
    List<TCinema> getCinema();

    /**
     * 搜索展示影院信息
     *
     * @param page
     * @param cinemaName
     * @param region
     * @param showTime
     * @param
     * @param service
     * @return
     */
    IPage<CinemaMessage> getCinemaMessage(Page<CinemaMessage> page, @Param("cinemaName") String cinemaName, @Param("region") String region, @Param("showTime") String showTime, @Param("service") String service);

    /**
     * 获取影院信息
     *
     * @param id
     * @return
     */
    @Select("SELECT  c.id,c.tel,c.cinema_name,c.address ,c.longitude,c.latitude,c.service,cd.file_path as cinemaPicture \n" +
            "FROM t_cinema AS c JOIN t_cinema_data AS cd ON c.id=cd.cinema_id WHERE c.state='2' AND cd.type='0' AND c.id=#{id}")
    CinemaInformention getCinemaMessagePicture(int id);

    /**
     * 获取当前影院热映的电影
     *
     * @param id
     * @return
     */

    List<MovieInformention> getCinemaMovie(@Param("id") int id, @Param("nowTime") String nowTime);

    /**
     * 查询出该场次的信息
     *
     * @param perId
     * @return
     */
    @Select("select * from t_perid where id=#{perId}")
    TPerid selectTperidById(int perId);

    /**
     * 获取优惠券信息
     *
     * @param id
     * @return
     */
    @Select("select * from t_discount where id=#{id} and end_date > #{nowTime} and state='1'")
    TDiscount getDiscountById(@Param("id") int id, @Param("nowTime") String nowTime);

    /**
     * 查询电影
     *
     * @param page
     * @param movieName
     * @return
     */
    @Select("SELECT m.id,m.`name`,m.type,m.movie_grade,md.file_path FROM t_movie AS m JOIN t_movie_data AS md ON m.id=md.movie_id WHERE m.`name` LIKE concat ('%',#{movieName},'%') and md.type='0'")
    IPage<MovieShow> getMovieByMovieName(Page<MovieShow> page, @Param("movieName") String movieName);

    /**
     * 获取影人信息
     *
     * @param page
     * @param peopleName
     * @return
     */
    @Select("SELECT p.id,p.`name`,pd.path FROM t_person AS p JOIN t_person_data AS pd ON p.id=pd.person_id WHERE p.`name`LIKE concat ('%',#{peopleName},'%')")
    IPage<PeopleShow> getMovieByPeopleName(Page<PeopleShow> page, @Param("peopleName") String peopleName);

    /**
     * 通过影院名获取信息
     *
     * @param page
     * @param cinemaName
     * @return
     */
    @Select("SELECT c.id,c.cinema_name,c.address,cd.file_path FROM t_cinema AS c JOIN t_cinema_data AS cd ON c.id=cd.cinema_id WHERE c.cinema_name LIKE concat ('%',#{cinemaName},'%')")
    IPage<CinemaShow> getMovieByCinemaName(Page<CinemaShow> page, String cinemaName);

    /**
     * 根据条件筛选电影
     *
     * @param page
     * @param movieType
     * @param movieTime
     * @return
     */
    IPage<MovieShow> getMovieByTypeTime(Page<MovieShow> page, @Param("movieType") String movieType, @Param("movieTime") String movieTime);

    /**
     * 查询用户的优惠券
     *
     * @param page
     * @param tel
     * @return
     */
    @Select("SELECT d.id,d.end_date,d.amount,d.state FROM t_user AS u JOIN t_discount AS d ON u.id=d.user_id WHERE u.tel=#{tel} AND d.end_date > #{nowTime} AND d.state='1'")
    IPage<DiscountShow> getDiscountByUserTel(Page<DiscountShow> page, @Param("tel") String tel, @Param("nowTime") String nowTime);

    /**
     * 根据电影id查询演员
     *
     * @param id
     * @return
     */
    @Select("select p.name from t_movie_person as mp join t_person as p on mp.person_id=p.id where mp.movie_id=#{id} and mp.type='0'")
    List<String> getAllActorByMovieId(int id);

    /**
     * 根据名字获取演员头像
     *
     * @param name
     * @return
     */
    @Select("select pd.path from t_person as p join t_person_data as pd on p.id=pd.person_id where p.name=#{name}")
    List<String> getAllPhotoActorsByName(String name);

    /**
     * 根据电影id查询导演
     *
     * @param id
     * @return
     */
    @Select("select p.name from t_movie_person as mp join t_person as p on mp.person_id=p.id where mp.movie_id=#{id} and mp.type='1'")
    List<String> getAllDirectorByMovieId(int id);

    /**
     * 根据用户tel获取用户id
     *
     * @param userTel
     * @return
     */
    @Select("select id from t_user where tel=#{userTel}")
    int getUserIdByUserTel(String userTel);

    /**
     * 根据场次id获取电影id，影厅id
     *
     * @param perId
     * @return
     */
    @Select("select p.movie_id as movieId ,a.cinema_id as cinemaId from t_perid as p join t_auditorium as a on a.id=p.auditorium_id where p.id=#{perId}")
    OrderMessage getMovieIdByPerId(int perId);

    /**
     * 添加订单
     *
     * @param orderMessage
     */
    @Insert("insert into t_order_movie (trade_nuo,user_id,check_code,cinema_id,movie_id,auditorium_id,perid_id,real_price,ticket_price,tip,number,refund,change_times,discount_id,create_time,state,status,statu)" +
            " values (#{tradeNuo},#{userId},#{checkCode},#{cinemaId},#{movieId},#{auditoriumId},#{peridId},#{realPrice},#{ticketPrice},#{tip},#{number},#{refund},#{change},#{discountId},#{data},#{state},#{status},#{statu})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addOrderMessage(OrderMessage orderMessage);

    /**
     * 修改优惠券状态
     *
     * @param i
     */
    @Update("update t_discount set state='0' where id=#{i}")
    void changeDiscountState(int i);

    /**
     * 查询会员情况
     *
     * @param userId
     * @param cinemaId
     * @param format
     * @return
     */
    @Select("select v.tip_discount,v.ticket_discount from t_user_vip as uv join t_vip as v on uv.cinema_id=v.cinema_id where uv.user_id=#{userId} and uv.cinema_id=#{cinemaId} and uv.vip_end_data > #{format} and uv.state='1' and v.state='1'")
    TVip getTvip(@Param("userId") int userId, @Param("cinemaId") int cinemaId, @Param("format") String format);

    /**
     * 添加座位进入订单详情表
     *
     * @param str
     * @param seat
     * @param format
     * @param
     */
    @Insert("insert into t_order_ticket (order_movie_id,seat_number,create_time,state)values(#{str},#{seat},#{format},'-1')")
    void addOrderTicket(@Param("str") int str, @Param("seat") String seat, @Param("format") String format);

    /**
     * 修改订单号
     *
     * @param tradeNuo
     */
    @Update("update t_order_movie set trade_nuo=#{tradeNuo} where id=#{id}")
    void updateTradeNuo(@Param("tradeNuo") String tradeNuo, @Param("id") int id);

    @Update("update t_order_movie set state=-1 where state=0 and id=#{id}")
    void zuofeiOrderMovie(int id);

    @Update("update t_order_ticket set state=-2 where state=-1 and order_movie_id=#{id}")
    void zuofeiOrderTicket(int id);

    @Select("select seat_stram from t_perid where id=#{peridId}")
    String getSetsByPerid(int peridId);

    @Update("update t_perid set seat_stram=#{s} where id=#{perId}")
    void changeSeatStram(String s, int perId);

    @Select("select * from t_order_movie where id=#{id}")
    TOrderMovie getOrderMovie(int id);
}
